package com.example.lj.uav.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lj.uav.R;
import com.example.lj.uav.view.base.BaseActivity;

import static com.example.lj.uav.MyApplication.mPref;

public class AuthorityActivity extends BaseActivity implements View.OnClickListener {
    private TextView mTvTitle;
    private LinearLayout mLlAuthority;
    private LinearLayout mLlPolicy;
    private ImageView mIvBack;
    private Button mBtnSubmit;
    private View mViewContent;
    private TextView mTvContent;
    private CheckBox mCb;
    private TextView mTvContentTitle;

    private int pageFlag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authority);
        initView();
        showFirstPage();
    }



    private void initView() {
        mTvTitle = findViewById(R.id.tv_authority_title);
        mLlAuthority = findViewById(R.id.ll_request_authority_by_one_key);
        mLlPolicy = findViewById(R.id.ll_user_agreement_privacy_policy);
        mIvBack = findViewById(R.id.iv_back);
        mBtnSubmit = findViewById(R.id.btn_authority_submit);
        mViewContent = findViewById(R.id.layout_user_agreement);
        mTvContent = mViewContent.findViewById(R.id.tv_agreement_content);
        mTvContentTitle = findViewById(R.id.tv_content_title);
        mCb = findViewById(R.id.cb_agree);
        mIvBack.setOnClickListener(this);
        mBtnSubmit.setOnClickListener(this);
        mCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    mBtnSubmit.setEnabled(true);
                }else{
                    mBtnSubmit.setEnabled(false);
                }
            }
        });
    }

    private void showFirstPage() {
        mLlAuthority.setVisibility(View.VISIBLE);
        mLlPolicy.setVisibility(View.GONE);
        mTvTitle.setText("权限申请");
        mBtnSubmit.setText("一键授权");
        mIvBack.setVisibility(View.INVISIBLE);
        mIvBack.setEnabled(false);
        pageFlag = 0;
    }

    private void showSecondPage() {
        mLlPolicy.setVisibility(View.VISIBLE);
        mLlAuthority.setVisibility(View.GONE);
        mTvTitle.setText("用户协议");
        mTvContentTitle.setText("用户协议");
        mBtnSubmit.setEnabled(false);
        mBtnSubmit.setText("下一步");
        mIvBack.setEnabled(true);
        mIvBack.setVisibility(View.VISIBLE);
        mTvContent.setText(getResources().getString(R.string.user_agreement_content));
        mCb.setChecked(false);
        pageFlag = 1;
    }

    private void showThirdPage() {
        mLlPolicy.setVisibility(View.VISIBLE);
        mLlAuthority.setVisibility(View.GONE);
        mTvTitle.setText("隐私权政策");
        mTvContentTitle.setText("隐私权政策");
        mBtnSubmit.setEnabled(false);
        mBtnSubmit.setText("下一步");
        mIvBack.setEnabled(true);
        mIvBack.setVisibility(View.VISIBLE);
        mTvContent.setText(getResources().getString(R.string.user_privacy_policy_content));
        mCb.setChecked(false);
        pageFlag = 2;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                switch (pageFlag){
                    case 1:
                        showFirstPage();
                        break;
                    case 2:
                        showSecondPage();
                        break;
                }
                break;
            case R.id.btn_authority_submit:
                switch (pageFlag){
                    case 0:
                        showSecondPage();
                        break;
                    case 1:
                        showThirdPage();
                        break;
                    case 2:
                        mPref.edit().putBoolean("is_authority1_commit",true).apply();
                        toAct(Authority_Activity2.class,true);
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }
}
